﻿using MediatR;
using SheepCultivation.Common.Domain.Sheep;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Domain.Week3;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.SheepManagement
{
    public class EliminatesSeachHandler : IRequestHandler<EliminatesSeachCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 羊只死亡仓储
        /// </summary>
        public readonly IBaseRepository<DeathManagement> DeathManagementRepository;
        /// <summary>
        /// 羊只淘汰仓储
        /// </summary>
        public readonly IBaseRepository<Eliminates> EliminatesRepository;
        /// <summary>
        /// 羊只类型仓储
        /// </summary>
        public readonly IBaseRepository<SheepTypes> SheepTypesRepository;
        /// <summary>
        /// 栋舍仓储
        /// </summary>
        public readonly IBaseRepository<Building> BuildingRepository;
        /// <summary>
        /// 栏位仓储
        /// </summary>
        public readonly IBaseRepository<Columns> ColumnsRepository;
        /// <summary>
        /// 品种仓储 
        /// </summary>
        public readonly IBaseRepository<Sheepvariety> SheepvarietyRepository;
        /// <summary>
        /// 种母配种仓储
        /// </summary>
        public readonly IBaseRepository<SheepHybridization> SheepHybridizationRepository;
        /// <summary>
        /// 出生信息仓储
        /// </summary>
        public readonly IBaseRepository<BirthMessage> BirthMessageRepository;
        /// <summary>
        /// 基本信息仓储
        /// </summary>
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        /// <summary>
        /// 日志
        /// </summary>
        public readonly ILogger<EliminatesSeachHandler> logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="deathManagementRepository">羊只死亡</param>
        /// <param name="EliminatesRepository">羊只淘汰</param>
        /// <param name="SheepTypesRepository">羊只类型</param>
        /// <param name="BuildingRepository">栋舍</param>
        /// <param name="ColumnsRepository">栏位</param>
        /// <param name="SheepvarietyRepository">品种</param>
        /// <param name="SheepHybridizationRepository">种母配种</param>
        /// <param name="BirthMessageRepository">出生信息</param>
        /// <param name="BasicInformationRepository">基本信息</param>
        /// <param name="logger">日志</param>
        public EliminatesSeachHandler(IBaseRepository<DeathManagement> deathManagementRepository, IBaseRepository<Eliminates> EliminatesRepository, IBaseRepository<SheepTypes> SheepTypesRepository, IBaseRepository<Building> BuildingRepository, IBaseRepository<Columns> ColumnsRepository, IBaseRepository<Sheepvariety> SheepvarietyRepository, IBaseRepository<SheepHybridization> SheepHybridizationRepository, IBaseRepository<BirthMessage> BirthMessageRepository, IBaseRepository<BasicInformation> BasicInformationRepository, ILogger<EliminatesSeachHandler> logger)
        {
            DeathManagementRepository = deathManagementRepository;
            this.EliminatesRepository = EliminatesRepository;
            this.SheepTypesRepository = SheepTypesRepository;
            this.BuildingRepository = BuildingRepository;
            this.ColumnsRepository = ColumnsRepository;
            this.SheepvarietyRepository = SheepvarietyRepository;
            this.SheepHybridizationRepository = SheepHybridizationRepository;
            this.BirthMessageRepository = BirthMessageRepository;
            this.BasicInformationRepository = BasicInformationRepository;
            this.logger = logger;
        }
        #endregion

        #region 羊只淘汰显示
        /// <summary>
        /// 羊只淘汰显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(EliminatesSeachCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {
                var data = new pagesAll() { };
                var list = (from a in EliminatesRepository.GetAll() //羊只淘汰
                            join b in BuildingRepository.GetAll() on a.BuildingId equals b.BuildingId //栋舍
                            join c in ColumnsRepository.GetAll() on a.ColumnsId equals c.ColumnsId    //栏位
                            join d in SheepvarietyRepository.GetAll() on a.SheepvarietyId equals d.SheepvarietyId //品种
                            join e in SheepHybridizationRepository.GetAll() on a.SheepHybridizationId equals e.SheepHybridizationId   //种母配种
                            join f in BirthMessageRepository.GetAll() on a.BreedingSheepArchiveId equals f.BreedingSheepArchiveId     //出生信息
                            join g in BasicInformationRepository.GetAll() on f.BreedingSheepArchiveId equals g.BreedingSheepArchiveId //基本信息
                            select new EliminatesDto()
                            {
                                //羊只淘汰
                                EliminatesId = a.EliminatesId,
                                EliminateState = a.EliminateState,
                                EliminatesRenamk = a.EliminatesRenamk,
                                EliminateMeasure = a.EliminateMeasure,
                                EliminatesTime = a.EliminatesTime,
                                //栋舍
                                BuildingId = b.BuildingId,
                                BuildingName = b.BuildingName,
                                //栏位
                                ColumnsId = c.ColumnsId,
                                ColumnsName = c.ColumnsName,
                                //品种
                                SheepvarietyId = d.SheepvarietyId,
                                SheepvarietyName = d.SheepvarietyName,
                                //种母配种
                                SheepHybridizationId = e.SheepHybridizationId,
                                SheepHybridizationReceiptNumber = e.SheepHybridizationReceiptNumber,
                                SheepHybridizationSerialNumber = e.SheepHybridizationSerialNumber,
                                BreedState = e.BreedState,
                                //出生信息
                                BirthMessageId = f.BirthMessageId,
                                BirthDate = f.BirthDate,
                                //基本信息（羊只耳号）
                                BreedingSheepArchiveId = g.BreedingSheepArchiveId,
                                Auricle = g.Auricle,
                                SheepSex = g.SheepSex,
                            }).ToList();
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
        #endregion
    }

    /// <summary>
    /// 分页
    /// </summary>
    public class pagesAll()
    {
        public int totalCount { get; set; }
        public int pageCount { get; set; }
        public List<EliminatesDto> data { get; set; }
    };

    public class EliminatesDto()
    {
        /// <summary>
        /// 淘汰管理ID
        /// </summary>
        public int EliminatesId { get; set; }
        /// <summary>
        /// 羊只耳号
        /// </summary>
        public int BreedingSheepArchiveId { get; set; }
        /// <summary>
        /// 栋舍Id
        /// </summary>
        public int BuildingId { get; set; }
        /// <summary>
        /// 栋舍名称
        /// </summary>
        public string? BuildingName { get; set; }
        /// <summary>
        /// 栏位Id
        /// </summary>
        public int ColumnsId { get; set; }
        /// <summary>
        /// 栏位名称
        /// </summary>
        public string? ColumnsName { get; set; }
        /// <summary>
        /// 品种Id
        /// </summary>
        public int SheepvarietyId { get; set; }
        /// <summary>
        /// 品种名称
        /// </summary>
        public string? SheepvarietyName { get; set; }
        /// <summary>
        /// 离场状态（离场状态）1：已离场 2：在场
        /// </summary>
        public int EliminateState { get; set; }
        /// <summary>
        /// 淘汰原因（下拉框）
        /// 1：空胎 2：其他 3：老龄 4：屡配不孕 5：习惯性流产 6：子宫脱 
        /// 7：子宫炎 8：肥胖 9：消瘦 10：精液质量差 11：拒配 12：无乳
        /// </summary>
        public int EliminatesRenamk { get; set; }
        /// <summary>
        /// 处理措施（下拉框）
        /// 1：销售 2：屠宰 3：无害化处理
        /// </summary>
        public int EliminateMeasure { get; set; }
        /// <summary>
        /// 淘汰时间（淘汰日期）
        /// </summary>
        public DateTime EliminatesTime { get; set; }
        /// <summary>
        /// 羊只类型（羊只类型表）
        /// </summary>
        public int SheepTypeId { get; set; }
        /// <summary>
        /// 单据号
        /// </summary>
        public string? Numberes { get; set; }
        /// <summary>
        /// 流水号
        /// </summary>
        public string? SerialNumber { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string? EliminatesRemark { get; set; }
        /// <summary>
        /// 种母分配Id
        /// </summary>
        public int SheepHybridizationId { get; set; }
        /// <summary>
        /// 单据号
        /// </summary>
        public string? SheepHybridizationReceiptNumber { get; set; }
        /// <summary>
        /// 流水号
        /// </summary>
        public string? SheepHybridizationSerialNumber { get; set; }
        /// <summary>
        /// 繁殖状态
        /// </summary>
        public string? BreedState { get; set; }
        /// <summary>
        /// 出生信息Id
        /// </summary>
        public int BirthMessageId { get; set; }
        /// <summary>
        /// 出生日期
        /// </summary>
        public DateTime BirthDate { get; set; }
        /// <summary> 
        /// 耳号
        /// </summary>
        public string Auricle { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        public int SheepSex { get; set; }
    };

}
